//
//  ETBaseVC.m
//  OilUnion
//
//  Created by itte on 2016/11/10.
//  Copyright © 2016年 funmain. All rights reserved.
//

#import "FSBaseVC.h"

@interface FSBaseVC ()

@end

@implementation FSBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化 返回按钮
    [self initNavigationLeftButton];

    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = false;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadCache];
}

#pragma mark - 控制器的一些操作
-(void)backup
{
    [self.navigationController popViewControllerAnimated:NO];
}
/**
 *初始化导航栏的返回按钮，如果不需要返回按钮，请在子类中重写
 */
-(void)initNavigationLeftButton
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backup)];
}
// 加载缓存
-(void)loadCache
{
}


#pragma mark - push & pop & dismiss view controller
-(void)pushViewController:(NSString *)className
               withParams:(NSDictionary *)paramDict
{
    UIViewController *pushedViewController = [[NSClassFromString(className) alloc] init];
    if (![pushedViewController isKindOfClass:[UIViewController class]])
    {
        NSLog(@"class[%@] is not ViewController!", className);
        return ;
    }
    else if ([pushedViewController isKindOfClass:[FSBaseVC class]])
    {
        if(paramDict != nil){
            [(FSBaseVC *)pushedViewController setParams:[NSDictionary dictionaryWithDictionary:paramDict]];
        }
        else{
            [(FSBaseVC *)pushedViewController setParams:nil];
        }
    }
    pushedViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:pushedViewController animated:NO];
}

-(void)pushViewController:(NSString *)className
{
    [self pushViewController:className withParams:nil];
}

-(void)presentViewcontroller:(NSString *)className animated:(BOOL)flag
{
    UIViewController *pushedViewController = [[NSClassFromString(className) alloc] init];
    if (![pushedViewController isKindOfClass:[UIViewController class]])
    {
        NSLog(@"class[%@] is not ViewController!", className);
        return ;
    }
    [self presentViewController:pushedViewController animated:flag completion:^{

    }];
}

- (void)popViewController
{
    // 如果是导航控制器
    if (self.navigationController)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)popToRootViewController
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
